package com.example.societiesSystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.societiesSystem.controller.UserController;
import com.example.societiesSystem.dto.TeamsResponseDTO;
import com.example.societiesSystem.entity.Teams;
import com.example.societiesSystem.entity.User;
import com.example.societiesSystem.mapper.TeamsMapper;
import com.example.societiesSystem.service.ISocietiesTypeService;
import com.example.societiesSystem.service.ITeamsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.societiesSystem.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 社团信息表 服务实现类
 * </p>
 *
 * @author 周欢
 * @since 2025-05-27
 */
@Service
public class TeamsServiceImpl extends ServiceImpl<TeamsMapper, Teams> implements ITeamsService {

    @Resource
    private IUserService userService;

    @Resource
    private ISocietiesTypeService societiesTypeService;

    @Override
    public IPage<TeamsResponseDTO> pageAll(Long pageIndex, Long pageSize, String token, String name, String typeId) {
        IPage<Teams> page = new Page<>(pageIndex, pageSize);
        LambdaQueryWrapper<Teams> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Teams::getUserId, token);
        queryWrapper.like(CharSequenceUtil.isNotBlank(name), Teams::getName, name);
        queryWrapper.eq(CharSequenceUtil.isNotBlank(typeId), Teams::getTypeId, typeId);
        IPage<Teams> teamsIPage = this.page(page, queryWrapper);
        List<TeamsResponseDTO> responseDTOS = teamsIPage.getRecords().stream().map(teams ->
                {
                    TeamsResponseDTO teamsResponseDTO = new TeamsResponseDTO();
                    BeanUtil.copyProperties(teams, teamsResponseDTO);
                    teamsResponseDTO.setUserName(userService.getNameById(Long.valueOf(teams.getUserId())));
                    teamsResponseDTO.setTypeName(societiesTypeService.getNameById(Long.valueOf(teams.getTypeId())));
                    return teamsResponseDTO;
                }
        ).collect(Collectors.toList());
        IPage<TeamsResponseDTO> teamsResponseDTOIPage = new Page<>();
        teamsResponseDTOIPage.setRecords(responseDTOS);
        teamsResponseDTOIPage.setTotal(teamsIPage.getTotal());
        teamsResponseDTOIPage.setSize(teamsIPage.getSize());
        teamsResponseDTOIPage.setCurrent(teamsIPage.getCurrent());
        teamsResponseDTOIPage.setPages(teamsIPage.getPages());
        return teamsResponseDTOIPage;
    }

    @Override
    public String getNameById(Long id) {
        List<Teams> teams = this.getBaseMapper().selectList(new LambdaQueryWrapper<Teams>().eq(Teams::getId, id));
        if (CollUtil.isNotEmpty(teams)) {
            return teams.get(0).getName();
        }
        return "";
    }

}
